package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class DodajTypUprawnieniaInputFrame  extends JFrame implements ActionListener{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2138837841557837926L;
	JTextField text1;
	JButton button1, button2;
	String n;
	
	public DodajTypUprawnieniaInputFrame () throws HeadlessException {
		super("Dodaj typ uprawnienia");
	}
	
	public void start()
	
    {
		JLabel label1 = new JLabel("Nazwa typu uprawnienia: ");
	    text1 = new JTextField(20);
	    button1 = new JButton("Dodaj");
	    button2 = new JButton("Zamknij");

	    this.add(label1, BorderLayout.WEST);
	    this.add(text1, BorderLayout.EAST);

	    this.add(button1);
	    this.add(button2);
	    button1.addActionListener(this );	   
	    button2.addActionListener(this );
		
	     setLayout(new FlowLayout());
	     setSize(300,130);
	     setVisible(true);
	     Toolkit kit = Toolkit.getDefaultToolkit();
	     Dimension screenSize = kit.getScreenSize();
	     setLocation((screenSize.width-300)/2, (screenSize.height-130)/2);
	     setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand() == "Zamknij"){
			this.dispose();
		}
		else if(e.getActionCommand() == "Dodaj"){
			try {
			     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			     Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=ABSI", "Admin","Admin");
			     CallableStatement statement = conn.prepareCall("EXEC DodajTypUprawnienia ?"); 
			     	n = text1.getText(); 
			     statement.setString(1,n);
			     statement.execute();

			     conn.close();
			  } catch (Exception ex) {
			     ex.printStackTrace();
			  }
		}
		text1.setText("");
		
	}

}

